<template>
  <div>
    <!------------------------面包屑--------------------------->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-s-management"></i>班级管理</el-breadcrumb-item>
      <el-breadcrumb-item>专业管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!------------------------添加专业-------------------------->
    <el-row style="text-align: left;margin: 10px 0">
      <el-button type="primary" size="medium" icon="el-icon-plus" @click="$router.push('/major/add-new')">添加专业</el-button>
    </el-row>

    <!------------------------专业表单--------------------------->
    <template>
      <el-table
          :data="MajorData"
          stripe
          border
          style="width: 100%">
        <el-table-column
            prop="id"
            label="专业ID"
            width="80"
            align="center">
        </el-table-column>
        <el-table-column
            prop="majorName"
            label="专业名称"
            header-align="center"
            align="center"
            show-overflow-tooltip
            width="150">
          <template slot-scope="scope">
            <!-- --------------------------------------------跳转对应专业超链接-------------------------------------------- -->
            <a  style="text-decoration: none;color: #409EFF;cursor:pointer"
                @click="goToQuestion(scope.row.id)">
              {{ scope.row.majorName }}</a>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="gmtCreate"
            show-overflow-tooltip
            label="创建时间">
        </el-table-column>
        <el-table-column
            align="center"
            prop="gmtModified"
            show-overflow-tooltip
            label="修改时间">
        </el-table-column>
        <el-table-column
            label="操作"
            width="150"
            align="center">

          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" circle @click="openEditDialog(scope.row)"></el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" circle
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  ------------------------分页器------------------------  -->
      <div style="text-align: right; margin: 10px auto">
        <el-pagination
            @current-change="changePage"
            layout="total, prev, pager, next"
            :total="total"
            :current-page="currentPage"
            :page-size="pageSize">
        </el-pagination>
      </div>
    </template>
    <!-- ------------------------Dialog 弹出对话框 嵌套表单版------------------------ -->
    <div style="margin: 0 auto">
      <el-dialog title="修改专业" :visible.sync="dialogFormVisible" width="480px">
        <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px" style="margin: -10px 0 -28px 0">
          <el-form-item label="专业id" prop="id">
            <el-input disabled v-model="editForm.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="专业名称" prop="majorName">
            <el-input v-model="editForm.majorName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitEditForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
let localJwt = localStorage.getItem('localJwt');
export default {
  data() {
    return {
      MajorData:[],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      rules: {
        majorName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        id: [
          {required: true, message: '请输入专业id', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/,message: "专业id必须在0~99之间",trigger: 'blur'}
        ]
      },
      //Dialog属性
      dialogFormVisible: false,
      editForm: {
        id: '',
        name: '',
      },
    }
  },
  methods:{
    /*跳转对应专业的试题管理*/
    goToQuestion(id){
      let path = '/class?majorId=' + id;
      this.$router.push(path);
    },
    openEditDialog(major){
      let url = 'http://localhost:9284/major/standard?id=' + major.id;
      console.log('url =' + url);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadAlbumList();
            }
          })
        }
      });
    },
    submitEditForm(){
      let url = 'http://localhost:9284/major/update';
      console.log('url=' + url);

      let  formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000){
          this.$message({
            message:'修改成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAlbumList();
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else if(jsonResult == 40400){
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确定',
            callback:action => {// eslint-disable-line no-unused-vars
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        }else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确定',
            callback:action => {// eslint-disable-line no-unused-vars
            }
          });
        }
      });
    },
    openDeleteConfirm(major){
      this.$confirm('此操作将永久删除【' + major.id + '-' + major.majorName + '】相册，是否继续？','提示',{
        confirmButtonText: '确认',
        cancelButtonText:'取消',
        type: "warning",
      }).then(() =>{
        this.handleDelete(major);
      }).catch(() =>{
      });
    },
    handleDelete(major) {
      let url = 'http://localhost:9284/major/delete';
      console.log('url = ' + url);

      let formData = 'id=' + major.id;
      console.log('formData = ' + formData);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除相册成功！',
            type: 'success'
          });
          this.loadMajorList();
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        }  else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
              this.loadMajorList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          });
        }
      });
    },
    changePage(value){
      this.$router.replace('?page=' + value);
      this.loadMajorList();
    },
    loadMajorList(){
      let page = this.$router.currentRoute.query.page;
      if (!page){
        page = 1;
      }
      console.log('localJwt =' + localJwt);
      let url = 'http://localhost:9284/major/list?page=' + page;
      console.log('url =' + url);

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult.state);
        console.log(jsonResult.data.list);
        if (jsonResult.state == 20000){
          this.MajorData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
            }
          })
        }
      });
    },
  },
  mounted() {
    this.loadMajorList();
  }
}
</script>

<style scoped>

</style>
